package com.lagou.controller;

import com.github.pagehelper.PageInfo;
import com.lagou.common.Const;
import com.lagou.common.ResponseCode;
import com.lagou.common.ServerResponse;
import com.lagou.model.User;
import com.lagou.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
@Transactional
public class UserController {

    @Autowired
    private IUserService iUserService;


    /**
     * 用户登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(String username, String password, HttpSession session){

        //调用service方法

        ServerResponse<User> response =iUserService.login(username, password);
        //判断是否登录成功，如果登录成功，把当前用户放到session中

        if (response.getStatus() == ResponseCode.SUCCESS.getCode()) {
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        
        //返回ServerResponse
        return response;
    }



    @RequestMapping(value = "list.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<PageInfo> list(@RequestParam(value = "pageNum",defaultValue = "1") int pageNum,
                                         @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){

        ServerResponse<PageInfo>  users =  iUserService.getUsers(pageNum,pageSize);

        return users;
    }



    @RequestMapping("add.do")
    @ResponseBody
    public ServerResponse<String> add(User user){
        return iUserService.add(user);
    }


    @RequestMapping(value = "deleteUser.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> delete(int id){

        return iUserService.deleteByPrimary(id);
    }



    @RequestMapping(value = "logout.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> logOut(HttpSession session){

        try {
            session.removeAttribute(Const.CURRENT_USER);
            return ServerResponse.createBySuccessMsg("注销成功");
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByErrorMsg("注销失败");
        }

    }


    /**
     * 根据用户ID查询用户详情
     * @param id
     * @return
     */
    @RequestMapping(value = "getInformation.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> queryUserById(int id){
        ServerResponse<User> response  =  iUserService.getInformation(id);
        return response;
    }


    @RequestMapping("update.do")
    @ResponseBody
    public ServerResponse<String> update(User user){
        return iUserService.update(user);
    }




}
